package com.ustb.util;
import com.sun.org.apache.xpath.internal.SourceTree;
import multichain.command.*;
import multichain.object.Address;
import multichain.object.BalanceAsset;
import multichain.object.BalanceAssetBase;
import multichain.object.TransactionWallet;
import com.ustb.util.SystemInfo;
import com.ustb.util.HexUtil;

import java.io.*;
import java.util.*;
    public class MultiChainUtil {
        private static String MULTICHAIN_SERVER_IP = SystemInfo.getProperties().get("MULTICHAIN_SERVER_IP");
        private static String MULTICHAIN_SERVER_PORT = SystemInfo.getProperties().get("MULTICHAIN_SERVER_PORT");
        private static String MULTICHAIN_SERVER_LOGIN = SystemInfo.getProperties().get("MULTICHAIN_SERVER_LOGIN") ;
        private static String MULTICHAIN_SERVER_PASSWORD = SystemInfo.getProperties().get("MULTICHAIN_SERVER_PASSWORD") ;
        private static MultiChainCommand multiChainCommand = new MultiChainCommand(MULTICHAIN_SERVER_IP,MULTICHAIN_SERVER_PORT,MULTICHAIN_SERVER_LOGIN,MULTICHAIN_SERVER_PASSWORD);

       /*public static void main(String[] args) throws Exception {
            List<BalanceAssetBase> listBalanceAsset = new ArrayList<>();
           String name = "asset1";
           double qty = 100;
           String comment = null;
           BalanceAssetBase balanceAssetBase = new BalanceAssetBase();
           balanceAssetBase.setName(name);
           balanceAssetBase.setQty(qty);
           balanceAssetBase.setComment(comment);
           listBalanceAsset.add(balanceAssetBase);
            try {

            }catch (Exception e){
                e.printStackTrace();
            }

        }*/
        public static  List<String> getAllAddress() throws Exception{
            List<String> ls = new ArrayList<String>();
            AddressCommand addressCommand = multiChainCommand.getAddressCommand();
            ls = addressCommand.getAddresses();
            return ls;
        }
        public static  String getNewAddress() throws Exception {
            String str = null;
            AddressCommand addressCommand = multiChainCommand.getAddressCommand();
            str = addressCommand.getNewAddress();
            return str;
        }
        public static String  newAssert(String address, String assetName, float quantity, float unit) throws  Exception {
            String str;
            IssueCommand issueCommand = multiChainCommand.getIssueCommand();
            str = issueCommand.issue(address,assetName,quantity,unit);
            return str;
        }
        public static String  addAssert(String address, String assetName, int quantity) throws Exception{ String str = null;
            IssueCommand issueCommand = multiChainCommand.getIssueCommand();
            str = issueCommand.issueMore(address,assetName,quantity);
            return null;
        }
        public static List<BalanceAsset> getTotalBalances() throws Exception{
            List<BalanceAsset> listBalanceAsset = new ArrayList();
            BalanceCommand balanceCommand = multiChainCommand.getBalanceCommand();
            listBalanceAsset = balanceCommand.getTotalBalances();

         /*   System.out.println(listBalanceAsset.get(0).getName());
            System.out.println(listBalanceAsset.get(0).getAssetref());
            System.out.println(listBalanceAsset.get(0).getQty());*/
            return  listBalanceAsset;

        }
        public static  String sendAssetToAddress(String address, String assetName, float quantity) throws Exception{
            String sendAssetToAddress = "";
            IssueCommand issueCommand = multiChainCommand.getIssueCommand();
            sendAssetToAddress = issueCommand.sendAssetToAddress(address,assetName,quantity);
            return sendAssetToAddress;
        }
        //所有交易信息 ，count = 10 遍历
        public static List<TransactionWallet> listWalletTransaction() throws Exception{
            List<TransactionWallet> list = null;
            WalletTransactionCommand walletTransactionCommand = multiChainCommand.getWalletTransactionCommand();
            list = walletTransactionCommand.listWalletTransaction();
            return list;
        }
        //最新交易信息，count = 1
        public static List<TransactionWallet> listWalletTransaction(long count) throws Exception{
            List<TransactionWallet> list = null;
            WalletTransactionCommand walletTransactionCommand = multiChainCommand.getWalletTransactionCommand();
            list = walletTransactionCommand.listWalletTransaction(count);
            /*
            * System.out.println(list.get(0).getMyaddresses().get(0));
                System.out.println(list.get(0).getAddresses());
                System.out.println(list.get(0).getBalance().getAssets().get(0).getQty());
                System.out.println(list.get(0).getBalance().getAssets().get(0));
                System.out.println(list.get(0).getData());
                System.out.println(list.get(0).getTime());*/
            return list;
        }
        public static  String sendWithMetaData(String address, List<BalanceAssetBase> assets, String hexMetaData) throws Exception {
            String stringSendWithMetaData = "";
            WalletTransactionCommand walletTransactionCommand = multiChainCommand.getWalletTransactionCommand();
            stringSendWithMetaData = walletTransactionCommand.sendWithMetaData(address,assets,hexMetaData);
            return stringSendWithMetaData;
        }
        /*
        * 交易和备注*/
        public static String sendWithDataFrom(String fromAddress, String toAddress, String assetName, Integer assetValue, String metadata) throws Exception{
            String  stringSendWithDataFrom = "";
            WalletTransactionCommand walletTransactionCommand = multiChainCommand.getWalletTransactionCommand();
            stringSendWithDataFrom = walletTransactionCommand.sendWithDataFrom(fromAddress,toAddress,assetName,assetValue,metadata);
       /*     try{

            }catch (Exception e){
                System.out.println("余额不足");
                System.out.println("当前余额 = "+listBalanceAsset.get(1).getQty());
            }*/

            return  stringSendWithDataFrom;
        }
        public  static String getPrivkey(String privkey) throws Exception{
            String string = "";
            KeyCommand keyCommand = multiChainCommand.getKeyCommand();
            string = (String) keyCommand.getPrivkey(privkey);
            return string;
        }

    }
